package com.gitee.wsl.coroutines.collections.sequences

import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.createCoroutine

interface SuspendingIterator<out T> {
    suspend operator fun hasNext(): Boolean
    suspend operator fun next(): T
}

fun <T> suspendingIterator(
    context: CoroutineContext = EmptyCoroutineContext,
    block: suspend SuspendingSequenceScope<T>.() -> Unit
): SuspendingIterator<T> =
    SuspendingIteratorCoroutine<T>(context).apply {
        nextStep = block.createCoroutine(receiver = this, completion = this)
    }